/**
 * 绘制三维圆柱区域
 */
export function drawCylinderArea(viewer, lon, lat, minHeight, maxHeight, radius, color, alpha = 0.5) {
  const height = maxHeight - minHeight; // 计算圆柱的高度

  const entity = viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(lon, lat, minHeight + height / 2), // 圆柱体中心位置
    cylinder: {
      length: height,
      topRadius: radius,
      bottomRadius: radius,
      material: Cesium.Color[color.toUpperCase()].withAlpha(alpha)
    }
  });

  return entity; // 返回绘制的实体
}


/**
 * 绘制三维多边体建筑物
 */
export function drawBuildingPolygon(viewer, groundPositions, topPositions, color = 'BLUE', alpha = 0.5) {
  const hierarchy = new Cesium.PolygonHierarchy(groundPositions);

  // 绘制多边体
  const entity = viewer.entities.add({
    polygon: {
      hierarchy: hierarchy,
      perPositionHeight: true, // 根据每个位置的高度来绘制多边体
      extrudedHeight: topPositions[0].height, // 顶面的高度
      material: Cesium.Color[color.toUpperCase()].withAlpha(alpha),
      outline: true,
      outlineColor: Cesium.Color.BLACK
    }
  });

  return entity; // 返回绘制的建筑物实体
}

/**
 * 解析建筑物数据并绘制多边体建筑物
 * @param viewer Cesium viewer实例
 * @param buildingData Array 包含建筑物多个点的数组，包含X坐标, Y坐标, 高度
 */
export function drawBuildings(viewer, buildingData) {
  let groundPositions = [];
  let topPositions = [];
  
  buildingData.forEach(point => {
    const position = Cesium.Cartesian3.fromDegrees(point.X坐标, point.Y坐标, 0);
    const topPosition = Cesium.Cartesian3.fromDegrees(point.X坐标, point.Y坐标, point.高度);

    groundPositions.push(position);
    topPositions.push(topPosition);
  });

  // 绘制建筑物多边体
  drawBuildingPolygon(viewer, groundPositions, topPositions);
}
